use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(PageTranslations::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(PageTranslations::Id)
                            .string()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(PageTranslations::PageId).string().not_null())
                    .col(ColumnDef::new(PageTranslations::Locale).string().not_null())
                    .col(ColumnDef::new(PageTranslations::Title).string().not_null())
                    .col(ColumnDef::new(PageTranslations::Content).text())
                    .col(ColumnDef::new(PageTranslations::Template).text())
                    .col(ColumnDef::new(PageTranslations::MetaTitle).string())
                    .col(ColumnDef::new(PageTranslations::MetaDescription).text())
                    .col(ColumnDef::new(PageTranslations::MetaKeywords).string())
                    .col(
                        ColumnDef::new(PageTranslations::CreatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(PageTranslations::UpdatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .index(
                        Index::create()
                            .name("idx_page_translations_page_id_locale_unique")
                            .col(PageTranslations::PageId)
                            .col(PageTranslations::Locale)
                            .unique(),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_page_translations_page_id")
                            .from(PageTranslations::Table, PageTranslations::PageId)
                            .to(Pages::Table, Pages::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(PageTranslations::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum PageTranslations {
    Table,
    Id,
    PageId,
    Locale,
    Title,
    Content,
    Template,
    MetaTitle,
    MetaDescription,
    MetaKeywords,
    CreatedAt,
    UpdatedAt,
}

#[derive(DeriveIden)]
enum Pages {
    Table,
    Id,
}
